package com.kasax.skinchange

import android.content.Context
import android.content.pm.PackageManager
import android.content.res.Resources
import androidx.core.content.ContextCompat
import com.kasax.skinchange.utils.LogUtils

class SkingLoader {

    private var skinResource: Resources? = null

    private var skinPkgName: String? = null

    companion object {
        val instance = SkingLoader()
    }

    fun getTextColor(context: Context, resId: Int): Int {
        //找到插件工程的对应资源id
        val identifier = getIdentifier(context, resId)
        if (skinResource == null || identifier <= 0) {
            return ContextCompat.getColor(context, resId)
        }
        return skinResource!!.getColor(identifier)
    }

    fun getText(context: Context, resId: Int): String {
        //找到插件工程的对应资源id
        val identifier = getIdentifier(context, resId)
        if (skinResource == null || identifier <= 0) {
            return context.getString(resId)
        }
        //获取插件工程的资源
        return skinResource!!.getString(identifier)
    }

    private fun getIdentifier(context: Context, resId: Int): Int {
        //主工程资源id->资源名字、类型->插件包中的资源id
        //R.color.black
        //black
        val resourceEntryName = context.resources.getResourceEntryName(resId)
        //color
        val resourceTypeName = context.resources.getResourceTypeName(resId)
        return skinResource?.getIdentifier(resourceEntryName, resourceTypeName, skinPkgName) ?: 0
    }

    fun loaderResourceWithNoReflection(context: Context, skinPath: String) {
        try {
            val packageArchiveInfo = context.packageManager.getPackageArchiveInfo(
                skinPath, PackageManager.GET_ACTIVITIES or PackageManager.GET_META_DATA or PackageManager.GET_SERVICES or PackageManager.GET_PROVIDERS
            )
            if (packageArchiveInfo == null) {
                LogUtils.i(javaClass, "loadResource: app load fail")
                return
            }

            skinPkgName = packageArchiveInfo.packageName
            packageArchiveInfo.applicationInfo.publicSourceDir = skinPath
            packageArchiveInfo.applicationInfo.sourceDir = skinPath
            skinResource = context.packageManager.getResourcesForApplication(packageArchiveInfo.applicationInfo)
        } catch (e: Exception) {
            LogUtils.e(javaClass, e)
        }
    }

    fun reset() {
        skinResource = null
        skinPkgName = null
    }


    fun getText(context: Context, attrName: String, attrType: String): String {
        val resourceId = skinResource?.getIdentifier(attrName, attrType, skinPkgName) ?: 0
        if (resourceId <= 0) {
            return context.getString(context.resources.getIdentifier(attrName, attrType, context.packageName))
        }
        //获取插件工程的资源
        return skinResource!!.getString(resourceId)
    }

    fun getTextColor(context: Context, attrName: String, attrType: String): Int {
        val resourceId = skinResource?.getIdentifier(attrName, attrType, skinPkgName) ?: 0
        if (resourceId <= 0) {
            return ContextCompat.getColor(context, context.resources.getIdentifier(attrName, attrType, context.packageName))
        }
        //获取插件工程的资源
        return skinResource!!.getColor(resourceId)
    }
}